package com.yanze.schoolinformation.viewModule.movie;

import android.arch.lifecycle.ViewModel;

import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.HotMovieBean;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;

import io.reactivex.disposables.Disposable;

/**
 * Created by MrZheng on 2018/4/24.
 */

public class DoubanMovieViewModel extends ViewModel{
    private OnMovieLoadListener listener;
    private Disposable mDisposable;

    public void DoubanMovieViewModel() {
    }

    public void setListener(OnMovieLoadListener listener) {
        this.listener = listener;
    }

    /**
     * 获取电影方法
     *
     * @param start 开始位置
     * @param count 获取个数
     */
    public void getMovie(int start, int count) {
        DataReqo.getInstant().getTop250Movie(C.url.API_DOUBAN, start, count)
                .subscribe(new DeforObserver<HotMovieBean>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        mDisposable = disposable;
                    }

                    @Override
                    public void _onNext(HotMovieBean entity) {
                        if (listener != null) {
                            listener.onSuccess(entity);
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        if (listener != null) {
                            listener.onFailure(msg);
                        }
                    }
                });
    }

    public void onDestroy() {
        listener = null;
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }
}
